package com.qike.huadongexcel.excelTools;

import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

public final class FileTools {


    /**
     * 复制目录
     * @param fromDir
     * @param toDir
     * @throws IOException
     */
    public static void copyDir(String fromDir,String toDir) throws IOException{
        //创建目录的File对象
        File dirSouce = new File(fromDir);
        //判断源目录是不是一个目录
        if (!dirSouce.isDirectory()) {
            //如果不是目录那就不复制
            return;
        }
        //创建目标目录的File对象
        File destDir = new File(toDir);
        //如果目的目录不存在
        if(!destDir.exists()){
            //创建目的目录
            destDir.mkdir();
        }
        //获取源目录下的File对象列表
        File[]files = dirSouce.listFiles();
        for (File file : files) {
            //拼接新的fromDir(fromFile)和toDir(toFile)的路径
            String strFrom = fromDir + File.separator + file.getName();
            System.out.println(strFrom);
            String strTo = toDir + File.separator + file.getName();
            System.out.println(strTo);
            //判断File对象是目录还是文件
            //判断是否是目录
            if (file.isDirectory()) {
                //递归调用复制目录的方法
                copyDir(strFrom,strTo);
            }
            //判断是否是文件
            if (file.isFile()) {
                System.out.println("正在复制文件："+file.getName());
                //递归调用复制文件的方法
                copyFile(strFrom,strTo);
            }
        }
    }
    /**
     * 复制文件
     * @param fromFile
     * @param toFile
     * @throws IOException
     */
    public static void copyFile(String fromFile,String toFile) throws IOException {
        //字节输入流——读取文件
        FileInputStream in = new FileInputStream(fromFile);
        //字节输出流——写入文件
        FileOutputStream out = new FileOutputStream(toFile);
        //把读取到的内容写入新文件
        //把字节数组设置大一些   1*1024*1024=1M
        byte[] bs = new byte[1*1024*1024];
        int count = 0;
        while((count = in.read(bs))!=-1){
            out.write(bs,0,count);
        }
        //关闭流
        in.close();
        out.flush();
        out.close();
    }

    /**
     * 获取文件夹下所有的文件
     * @param filepath
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static List<File> readfile(String filepath) throws FileNotFoundException, IOException {
        List<File> fileList = new ArrayList<>();
        try {
            File file = new File(filepath);
            if (!file.isDirectory()) {
                fileList.add(file);
                System.out.println("文件");
                System.out.println("path=" + file.getPath());
                System.out.println("absolutepath=" + file.getAbsolutePath());
                System.out.println("name=" + file.getName());

            } else if (file.isDirectory()) {
                System.out.println("文件夹");
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(filepath + "\\" + filelist[i]);
                    if (!readfile.isDirectory()) {
                        fileList.add(readfile);
                        System.out.println("path=" + readfile.getPath());
                        System.out.println("absolutepath="
                                + readfile.getAbsolutePath());
                        System.out.println("name=" + readfile.getName());

                    } else if (readfile.isDirectory()) {
                        readfile(filepath + "\\" + filelist[i]);
                    }
                }

            }


        } catch (FileNotFoundException e) {
            System.out.println("readfile()   Exception:" + e.getMessage());
        } finally {
            return fileList;
        }
    }

    /**
     * 获取文件的创建时间
     * @param filePath
     * @return
     */
    public static Long getFileCreateTime(String filePath){
        File file = new File(filePath);
        try {
            Path path= Paths.get(filePath);
            BasicFileAttributeView basicview= Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS );
            BasicFileAttributes attr = basicview.readAttributes();
            return attr.creationTime().toMillis();
        } catch (Exception e) {
            e.printStackTrace();
            return file.lastModified();
        }
    }


    /**
     * 获取文件的创建时间
     * @param filePath
     * @return
     */
    public static void runHong(String filepath, Boolean visible, Boolean readonly,String hongName){
        JacobExcelTool tool = null;
        try {
            tool = new JacobExcelTool();
            //打开
            tool.OpenExcel(filepath, visible, readonly);
//            Dispatch sheet = tool.getSheetByName("Sheet1");
//            for (int i = 2; i <= 7; i++) {
//                tool.setValue(sheet, "B" + i, "value", i * 1.2);
//            }
            //调用Excel宏

            //调用Excel宏
//        tool.callMacro("样式设置");
//            Variant num = tool.getValue("B6", sheet);
//            System.out.println(num);
            tool.callMacro(hongName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭并保存，释放对象
            tool.CloseExcel(true, true);
        }

    }

    /**
     * 删除文件
     * @param path
     */
    public static void  deleteFile(String path){
        File file = new File(path);
        file.deleteOnExit();
    }

}
